package com.jevons.geo_edit.model;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.jevons.muffin.util.UuidUtils;
import com.jevons.muffin.util.VerificationUtils;
import com.jevons.muffin.utilcode.GsonUtils;
import com.jevons.muffin.utilcode.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright 2020 JxGIS
 *
 * @file GetGisDataRes
 * @auth linaisheng
 * Created on 2023/9/21.
 * Description：
 */
public class GetGisDataRes {

    private GisProject info;

    private List<Layer> layerList;

    public GisProject getInfo() {
        return info;
    }

    public List<Layer> getLayerList() {
        return layerList;
    }

    public class Layer {

        public String id;
        public String infoId;

        public String layerName;
        public int type;
        public String fieldTempId;
        public int sort;     //排序
        public String layerStyle;      //图层样式
        public String labelStyle;      //标注样式

        public ArrayList<JsonObject> geometryList;
        public ArrayList<GisField> fieldList;


        public GisLayer generateLayer() {
            GisLayer gisLayer = new GisLayer(id, infoId, layerName, type, fieldTempId, sort, layerStyle, labelStyle);

            if (fieldList != null) {
                gisLayer.setFieldList(fieldList);
            }

            if (geometryList != null) {
                List<GisGeometry> geometries = new ArrayList<>();
                for (JsonObject geometryJsonObject : geometryList) {



                    String wkt = null;

                    if (geometryJsonObject.has("the_geom")) {
                        wkt = geometryJsonObject.remove("the_geom").getAsString();
                    }
                    if (StringUtils.isNotEmpty(wkt)) {
                        String featureId = null;
                        JsonObject property = new JsonObject();

                        if (geometryJsonObject.has("id")) {
                            featureId = geometryJsonObject.get("id").getAsString();
                        }
                        if (StringUtils.isEmpty(featureId)) {
                            featureId = UuidUtils.generateId();
                            property.addProperty("id", featureId);
                        }

                        GisGeometry gisGeometry = new GisGeometry(featureId, wkt);
                        if (fieldList != null && fieldList.size() > 0) {
                            for (GisField field : fieldList) {
                                String fieldId = field.getId();
                                String fieldType = field.getFieldType();
                                String fieldName = field.getFieldName();
                                JsonElement value;
                                if (geometryJsonObject.has(fieldName)) {
                                    value = geometryJsonObject.remove(fieldName);
                                    if (fieldType.equals("Date") && value.isJsonPrimitive()) {
                                        String date = value.getAsString();
                                        if (date.contains(" ")) {
                                            value = new JsonPrimitive(date.split(" ")[0]);
                                        }
                                    }
                                } else {
                                    value = JsonNull.INSTANCE;
                                }
                                property.add(fieldId, value);
                            }
                        }
                        gisGeometry.setProperty(property);

                        geometries.add(gisGeometry);
                    }
                }
                gisLayer.setGeometryList(geometries);
            }


            JsonObject layerStyleOb = GsonUtils.fromJson(layerStyle, JsonObject.class);
            JsonObject labelStyleOb = GsonUtils.fromJson(labelStyle, JsonObject.class);
            GisLayerStyle gisLayerStyle = new GisLayerStyle(infoId, id);

            JsonPrimitive pointType = getLayerStyleProperty(layerStyleOb,"pointType");
            if (pointType != null) {
                gisLayerStyle.setPointType(pointType.getAsInt());
            }
            JsonPrimitive pointSize = getLayerStyleProperty(layerStyleOb,"pointSize");
            if (pointSize != null) {
                gisLayerStyle.setPointSize(pointSize.getAsFloat());
            }
            JsonPrimitive pointStyle = getLayerStyleProperty(layerStyleOb,"pointStyle");
            if (pointStyle != null) {
                gisLayerStyle.setPointStyle(pointStyle.getAsInt());
            }

            JsonPrimitive strokeType = getLayerStyleProperty(layerStyleOb,"strokeType");
            if (strokeType != null) {
                gisLayerStyle.setLineType(strokeType.getAsInt());
            }
            JsonPrimitive strokeWidth = getLayerStyleProperty(layerStyleOb,"strokeWidth");
            if (strokeWidth != null) {
                gisLayerStyle.setLineWidth(strokeWidth.getAsFloat());
            }
            JsonPrimitive strokeColor = getLayerStyleProperty(layerStyleOb,"strokeColor");
            if (strokeColor != null) {
                gisLayerStyle.setLineColor(strokeColor.getAsString());
            }
            JsonPrimitive strokeOpacity = getLayerStyleProperty(layerStyleOb,"strokeOpacity");
            if (strokeOpacity != null) {
                gisLayerStyle.setLineOpacity(strokeOpacity.getAsFloat());
            }

            JsonPrimitive fillType = getLayerStyleProperty(layerStyleOb,"fillType");
            if (fillType != null) {
                gisLayerStyle.setFillType(fillType.getAsInt());
            }
            JsonPrimitive fillColor = getLayerStyleProperty(layerStyleOb,"fillColor");
            if (fillColor != null) {
                gisLayerStyle.setFillColor(fillColor.getAsString());
            }
            JsonPrimitive fillOpacity = getLayerStyleProperty(layerStyleOb,"fillOpacity");
            if (fillOpacity != null) {
                gisLayerStyle.setFillOpacity(fillOpacity.getAsFloat());
            }


            JsonPrimitive showKey = getLayerStyleProperty(labelStyleOb,"showKey");
            if (showKey != null) {
                String showKeyName = showKey.getAsString();
                String showAnnotationKey = "";
                if (StringUtils.isNotEmpty(showKeyName) && fieldList != null && !fieldList.isEmpty()) {
                    for (GisField field : fieldList) {
                        if (showKeyName.equals(field.getFieldName())) {
                            showAnnotationKey = field.getId();
                        }
                    }
                }
                gisLayerStyle.setShowAnnotationKey(showAnnotationKey);
            }
            JsonPrimitive fontSize = getLayerStyleProperty(labelStyleOb,"fontSize");
            if (fontSize != null) {
                String value = fontSize.getAsString();
                if (VerificationUtils.isNumeric(value)) {
                    gisLayerStyle.setFontSize(Float.parseFloat(value));
                }
            }
            JsonPrimitive fontFamily = getLayerStyleProperty(labelStyleOb,"fontFamily");
            if (fontFamily != null) {
                gisLayerStyle.setFontFamily(fontFamily.getAsString());
            }
            JsonPrimitive fontBold = getLayerStyleProperty(labelStyleOb,"fontBold");
            if (fontBold != null) {
                String value = fontBold.getAsString();
                if (VerificationUtils.isNumeric(value)) {
                    gisLayerStyle.setFontBold(Integer.parseInt(value));
                }
            }
            JsonPrimitive fontStyle = getLayerStyleProperty(labelStyleOb,"fontStyle");
            if (fontStyle != null) {
                String value = fontStyle.getAsString();
                if (VerificationUtils.isNumeric(value)) {
                    gisLayerStyle.setFontItalic(Integer.parseInt(value));
                }
            }
            JsonPrimitive fontFillColor = getLayerStyleProperty(labelStyleOb,"fontFillColor");
            if (fontFillColor != null) {
                gisLayerStyle.setFontFillColor(fontFillColor.getAsString());
            }
            JsonPrimitive fontStrokeColor = getLayerStyleProperty(labelStyleOb,"fontStrokeColor");
            if (fontStrokeColor != null) {
                gisLayerStyle.setFontStrokeColor(fontStrokeColor.getAsString());
            }
            JsonPrimitive fontStrokeWidth = getLayerStyleProperty(labelStyleOb,"fontStrokeWidth");
            if (fontStrokeWidth != null) {
                String value = fontStrokeWidth.getAsString();
                if (VerificationUtils.isNumeric(value)) {
                    gisLayerStyle.setFontStrokeWidth(Float.parseFloat(value));
                }
            }

            gisLayer.setMyLayerStyle(gisLayerStyle);

            return gisLayer;
        }


        private JsonPrimitive getLayerStyleProperty(JsonObject object, String propertyName) {
            if (object != null && object.has(propertyName)) {
                JsonElement jsonElement = object.get(propertyName);
                if (jsonElement.isJsonPrimitive()) {
                    return (JsonPrimitive) jsonElement;
                }
            }
            return null;
        }

    }




}